package jwxx.server;
import java.sql.ResultSet;
import java.sql.SQLException;
import jwxx.server.DbOperator;
import jwxx.server.SItem;
public class SItemSql {
	private int n;
	private ResultSet rs;
	
	public int insert(SItem cb)throws SQLException{
		String sql = "insert into item_table (id,fk_cloth_id,fk_trousers_id,fk_shoe_id,fk_ornaments_id,fk_weapon_id)values(?,?,?,?,?,?)";
		Object [] obj = new Object []{cb.getID(),cb.getFkClothId(),cb.getFkTrousersId(),cb.getFkShoeId(),cb.getFkOrnamentsId(),cb.getFkWeaponId()};
				
		DbOperator db = new DbOperator();
		try	{
			n=db.executeUpdate(sql,obj);
			}catch(SQLException e){
				e.printStackTrace();
			}	
		db.close();	
		return n;
		}
	
	public int delete(int id)throws SQLException{
		String sql = "delete from item_table where id=?";
		Object [] obj = new Object []{new Integer(id)};
		DbOperator db = new DbOperator();		
		try	{
			n=db.executeUpdate(sql,obj);
			}catch(SQLException e){
				e.printStackTrace();
			}	
		db.close();	
		return n;
	}	
	
	public ResultSet selectAllClass()throws SQLException{
		String sql="select * from item_table";
		DbOperator db = new DbOperator();
		try{
			rs=db.executeQuery(sql, new Object[]{});
		}catch(SQLException e){
			e.printStackTrace();
		}
		db.close();
		return rs;
	}
	
	public SItem selectById(int id)throws SQLException{		
		String sql2 = "select * from item_table where id=?";
		DbOperator db = new DbOperator();
		SItem cb = new SItem();
		try{
			rs = db.executeQuery(sql2, new Object[]{new Integer(id)});
			while(rs.next()){
				cb.setID(rs.getInt("id"));
				cb.setFkClothId(rs.getInt("fk_cloth_id"));
				cb.setFkTrousersId(rs.getInt("fk_trousers_id"));
				cb.setFkShoeId(rs.getInt("fk_shoe_id"));	
				cb.setFkOrnamentsId(rs.getInt("fk_ornaments_id"));
				cb.setFkWeaponId(rs.getInt("fk_weapon_id"));							
			}
		}catch(SQLException e){
			e.printStackTrace();
		}	
		db.close();
		return cb;
	
		}

}
